import Toast from '@vant/weapp/toast/toast';
import {sayTips} from './sayTips' 
// pages/orderManagement/orderManagement.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    OrdersProgress:[],
    OrdersTuiIing:[],
    OrdersDone:[],
    order:[],
    timer:''
  },
  changeOrderDone(e){
    wx.showLoading({
      title:"提交订单ing"
    })
    new Promise((resolve)=>{
      const {status=1} = e.target.dataset;
      wx.request({
        url: `http://localhost:5555/shop/orderStatusChange?orderid=${e.target.dataset.orderid}&status=${status}`,
        method:"get",
        success(res){
          resolve(res)
        }
      })
    }).then((res)=>{
      this.getShopOrder()
      wx.hideLoading()
      Toast.success('制作完成提交成功');
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.getShopOrder()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {
    clearInterval(this.data.timer);
    const timer = setInterval(()=>{
      this.getShopOrder()
    },2000)
    this.setData({
      timer
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    clearInterval(this.data.timer);
    const timer = setInterval(()=>{
      this.getShopOrder()
    },2000)
    this.setData({
      timer
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },
getShopOrder(){
  const id = wx.getStorageSync('shopid')
  wx.request({
    url: `http://localhost:5555/shop/shopOrder?shopid=${id}`,
    method:"get",
    success:(res)=>{
      let OrdersProgress = [];
      let OrdersDone = [];
      let OrdersTuiIing = []
      let data = res.data.data;
      for(let i =0;i<data.length;i++){
        if(data[i].status==0){
          OrdersProgress.push(data[i]);
        }else if(data[i].status=='1' || data[i].status=='3'){
          OrdersDone.push(data[i])
        }else if(data[i].status==2){
          OrdersTuiIing.push(data[i]);
        }
      }
      if((this.data.OrdersProgress.length+this.data.OrdersDone.length+this.data.OrdersTuiIing.length)<res.data.data.length && OrdersProgress.length!==0){
        Toast.success('您有新的订单');
        sayTips("你有新的订单请及时查收")
      }
      this.setData({
        OrdersProgress,
        OrdersDone,
        OrdersTuiIing
      })
    }
  })
},
  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})